package com.ebao.service.base.impl;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import com.ebao.dao.base.IBaseJpaDao;
import com.ebao.service.base.ICrudService;
import com.ebao.util.PageResult;

/**
 * @author snow
 * @date 2012-6-23
 * @param <P>
 */
public class CrudServiceImpl<P> implements ICrudService<P> {
	
	protected IBaseJpaDao<P> dao;
	
	public void setDao(IBaseJpaDao<P> dao) {
        this.dao = dao;
    }
	
	public void save(P obj) {
		dao.save(obj);
	}
	
	public void save(Collection<P> coll){
		for(P obj : coll){
			this.save(obj);
		}
	}

	public void update(P obj) {
		dao.update(obj);
	}
	
	public void delete(Serializable id) {
		if(id != null && !id.equals("")){
			dao.delete(id);
		}
	}
	
	public void delete(P obj){
		dao.delete(obj);
	}
	
	public void delete(Collection<P> coll){
		for(P obj : coll){
			this.delete(obj);
		}
	}
	
	public void delete(P[] arr){
		for(P obj : arr){
			this.delete(obj);
		}
	}

	public P getById(Serializable id) {
		return dao.getById(id);
	}
	
	public P getByJpql(String jpql, Object...objs){
		return dao.getByJpql(jpql, objs);
	}
	
	public List<P> getListByJpql(String jpql, Object... objs){
		return dao.query(jpql, objs);
	}
	
	public List<P> getByJpql(String jpql, int currentPage, int pageSize, Object... objs){
		return dao.query(jpql, (currentPage - 1) * pageSize, pageSize, objs);
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public PageResult<P> getPageByJpql(String jpql, int currentPage, int pageSize, Object... objs) {
		PageResult pageResult = new PageResult();
		
		int totalCount = dao.getCount("select count(*) " + jpql, objs);
		int totalPage = (totalCount % pageSize == 0) ? totalCount/pageSize : totalCount/pageSize + 1; 
		int cp = (currentPage <= totalPage) ? currentPage : totalPage; 
		
		List<P> contents = dao.query(jpql, (cp - 1) * pageSize, pageSize, objs);
		pageResult.setContents(contents);
		pageResult.setCurrentPage(cp);
		pageResult.setPageSize(pageSize);
		
		pageResult.setTotalCount(totalCount);
		
		return pageResult;
	}
	
	public int executeJpql(String jpql, Object... objs){
		return dao.batchUpdate(jpql, objs);
	}

	public int getCount(String jpql, Object... objs) {
		return dao.getCount(jpql, objs);
	}
	
	public int getCountBySql(String sql, Object... objs){
		return dao.executeNativeGetCount(sql, objs);
	}
	
	public int updateBySql(String sql, Object... objs){
		return dao.executeNativeUpdate(sql, objs);
	}
	
	@SuppressWarnings("rawtypes")
	public Object getBySql(String sql, Class resultClass, Object... objs){
		return dao.executeNativeQueryGetObj(sql, resultClass, objs);
	}
	
	@SuppressWarnings("rawtypes")
	public List getListBySql(String sql, Class resultClass, Object... objs){
		return dao.executeNativeQuery(sql, resultClass, objs);
	}
	
	@SuppressWarnings("rawtypes")
	public List getPageBySql(String sql, Class resultClass, int begin, int max, Object... objs){
		return dao.executeNativeQuery(sql, resultClass, begin, max, objs);
	}

}
